var goby = goby || {};

goby.GameLayer = cc.LayerColor.extend({
    isMouseDown: false,
    size:null,
    _tileMap:null,
    _background: null,
    ctor: function(){
        this._super();
        var size = cc.winSize;
        this._tileMap = new cc.TMXTiledMap('res/map/level_1.tmx');
        this._background = this._tileMap.getLayer('Background');
        this.addChild(this._tileMap, -1);
    }
   
});

goby.GameScene = cc.Scene.extend({
    isMouseDown: false,
    size:null,
    _tileMap:null,
    _background: null,
    _player: null,
    wordSize: null,
    space: null,
    level: 1, // Level
    pack: 1,  // Sub level
    ctor: function(level, pack){
        this._super();
        this.level = level;
        this.pack = pack;
    },
    initPlayer: function() {
        var objects = this._tileMap.getObjectGroup("Objects");
        var point = objects.getObjects();
        point = cc.p(Number(point[0]['x']), Number(point[0]['y']));
        var frames = [res.sleep_1, res.sleep_2, res.sleep_3, res.sleep_4, res.sleep_5];
        this._player = new goby.PlayerLayer(this.space,frames, point );
        var self = this;
        this._player.setCollided(function(p){
            var tiledCoord = self.positionToTileCoord(p);
            var gid = self._background.getTileGIDAt(tiledCoord);
            if(gid) {
                var property = self._tileMap.getPropertiesForGID(gid);
                if(property['collidable'] == 0) {
                    self.gameOver();
                    return false;
                }
            }
            return true;
        });
        this.addChild(this._player);
    },
    gameOver: function(){
        cc.log('game over!');
    },
    collided: function(point){
        var tiledCoord = this.positionToTileCoord(point);
        var gid = this._background.getTileGIDAt(tiledCoord);
        if(gid) {
            var property = this._tileMap.getPropertiesForGID(gid);
            if(property['collidable'] == 0) {
                this.gameOver();
                return false;
            }
        }
        return true;
    },
    initMap: function(){
        this._super();
        var size = cc.winSize;
        this._tileMap = new cc.TMXTiledMap('res/map/level_' + this.level + '_' + this.pack + '.tmx');
        this._background = this._tileMap.getLayer('Background');
        this.wordSize = cc.size(this._tileMap.getMapSize().width * this._tileMap.getTileSize().width,
                              this._tileMap.getMapSize().height * this._tileMap.getTileSize().height);
        var colorLayer = new cc.LayerColor(cc.color(227, 240, 188, 255), this.wordSize.width, this.wordSize.height);
        this.addChild(colorLayer, -1);
        this.addChild(this._tileMap, -1);

    },
    initPhysics:function() {
        //1. new space object
        this.space = this.space|| new cp.Space();
        //2. setup the  Gravity
        this.space.gravity = cp.v(0, 0); // -50 刚刚好
        var debugNode = new cc.PhysicsDebugNode(this.space);
        debugNode.visible = true;
        this.addChild(debugNode);
        // 3. set up Walls
        //var wallBottom = new cp.SegmentShape(this.space.staticBody,
        //    cp.v(-10, -10),// start point
        //    cp.v(4294967295, 80),// MAX INT:4294967295
        //    0);// thickness of wall
        //this.space.addStaticShape(wallBottom);
    },
    setCenter: function(point) {
        var size = cc.winSize;
        var x  = Math.max(point.x, size.width / 2);
        var y = Math.max(point.y, size.height / 2);
        x = Math.min(x, this.wordSize.width - size.width / 2);
        y = Math.min(y,  this.wordSize.height - size.height / 2);
        x = size.width / 2 - x;
        y = size.height / 2 - y;

        this.setPosition(cc.p(x, y));
    },
    positionToTileCoord: function(point){
        return cc.p(
            Math.round(point.x/this._tileMap.getTileSize().width),//x
            Math.round((this.wordSize.height - point.y)/ this._tileMap.getTileSize().height)//y
        );
    },
    onEnter: function() {
        this._super();
        this.initPhysics();
        this.initMap();
        this.initPlayer();
    }
});
